//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "OptionletStripper2.h"
using namespace Cephei::QL::Termstructures::Volatility::Optionlet;
#include <gen/QL/Termstructures/Volatility/Optionlet/OptionletStripper1.h>
#include <gen/QL/Termstructures/Volatility/Capfloor/CapFloorTermVolCurve.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Indexes/IborIndex.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Termstructures/Volatility/Capfloor/CapFloorTermVolSurface.h>
#include <gen/QL/Termstructures/Volatility/Optionlet/OptionletStripper.h>
using namespace Cephei::QL::Termstructures::Volatility::Capfloor;
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Indexes;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper2::COptionletStripper2 (Cephei::QL::Termstructures::Volatility::Optionlet::IOptionletStripper1^ optionletStripper1, Cephei::QL::Termstructures::Volatility::Capfloor::ICapFloorTermVolCurve^ atmCapFloorTermVolCurve) : COptionletStripper(COptionletStripper2::typeid)
{
    COptionletStripper1^ _CoptionletStripper1;
    CCapFloorTermVolCurve^ _CatmCapFloorTermVolCurve;
    try
    {
#ifdef HANDLE
        _phOptionletStripper2 = NULL;
#endif
        _CoptionletStripper1 = safe_cast<COptionletStripper1^> (optionletStripper1);
        _CoptionletStripper1->Lock();
        boost::shared_ptr<QuantLib::OptionletStripper1>& _optionletStripper1 = static_cast<boost::shared_ptr<QuantLib::OptionletStripper1>&> (_CoptionletStripper1->GetShared ()); 
        _CatmCapFloorTermVolCurve = safe_cast<CCapFloorTermVolCurve^> (atmCapFloorTermVolCurve);
        _CatmCapFloorTermVolCurve->Lock();
        Handle<QuantLib::CapFloorTermVolCurve>& _atmCapFloorTermVolCurve = static_cast<Handle<QuantLib::CapFloorTermVolCurve>&> (_CatmCapFloorTermVolCurve->GetHandle ()); 
        _ppOptionletStripper2 = new boost::shared_ptr<QuantLib::OptionletStripper2> (new QuantLib::OptionletStripper2 ( _optionletStripper1,  _atmCapFloorTermVolCurve ));
        SetOptionletStripper (boost::dynamic_pointer_cast<QuantLib::OptionletStripper> (*_ppOptionletStripper2));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CoptionletStripper1 != nullptr) _CoptionletStripper1->Unlock();
        if (_CatmCapFloorTermVolCurve != nullptr) _CatmCapFloorTermVolCurve->Unlock();
    }
}
Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper2::COptionletStripper2 (boost::shared_ptr<QuantLib::OptionletStripper2>& childNative, Object^ owner) : COptionletStripper(COptionletStripper2::typeid)
{
#ifdef HANDLE
	_phOptionletStripper2 = NULL;
#endif
	_ppOptionletStripper2 = &childNative;
    _ppOptionletStripper = new boost::shared_ptr<QuantLib::OptionletStripper> (boost::dynamic_pointer_cast<QuantLib::OptionletStripper> (*_ppOptionletStripper2));
}
Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper2::COptionletStripper2 (QuantLib::OptionletStripper2& childNative, Object^ owner) : COptionletStripper(COptionletStripper2::typeid)
{
#ifdef HANDLE
	_phOptionletStripper2 = NULL;
#endif
	_ppOptionletStripper2 = new boost::shared_ptr<QuantLib::OptionletStripper2> (&childNative);
    _ppOptionletStripper = new boost::shared_ptr<QuantLib::OptionletStripper> (boost::dynamic_pointer_cast<QuantLib::OptionletStripper> (*_ppOptionletStripper2));
    _OptionletStripper2Owner = owner;
    _OptionletStripperOwner = owner;
}

Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper2::COptionletStripper2 (COptionletStripper2^ copy) : COptionletStripper(COptionletStripper2::typeid)
{
#ifdef HANDLE
	_phOptionletStripper2 = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppOptionletStripper2 = new boost::shared_ptr<QuantLib::OptionletStripper2> (copy->GetShared());
        _ppOptionletStripper = new boost::shared_ptr<QuantLib::OptionletStripper> (boost::dynamic_pointer_cast<QuantLib::OptionletStripper> (*_ppOptionletStripper2));
    }
}
Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper2::COptionletStripper2 (PLATFORM::Type^ t) : COptionletStripper(COptionletStripper2::typeid)
{
#ifdef HANDLE
	_phOptionletStripper2 = NULL;
#endif
	if (!t->IsSubclassOf(COptionletStripper2::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper2::COptionletStripper2 (QuantLib::Handle<QuantLib::OptionletStripper2>& childNative, Object^ owner)  : COptionletStripper(COptionletStripper2::typeid)
{
	_phOptionletStripper2 = &childNative;
	_ppOptionletStripper2 = &static_cast<boost::shared_ptr<QuantLib::OptionletStripper2>>(childNative.currentLink());
    _ppOptionletStripper = new boost::shared_ptr<QuantLib::OptionletStripper> (boost::dynamic_pointer_cast<QuantLib::OptionletStripper> (*_ppOptionletStripper2));
    _OptionletStripper2Owner = owner;
}
Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper2::COptionletStripper2 (QuantLib::Handle<QuantLib::OptionletStripper2> childNative)  : COptionletStripper(COptionletStripper2::typeid)
{
	_phOptionletStripper2 = &childNative;
	_ppOptionletStripper2 = &static_cast<boost::shared_ptr<QuantLib::OptionletStripper2>>(childNative.currentLink());
    _ppOptionletStripper = new boost::shared_ptr<QuantLib::OptionletStripper> (boost::dynamic_pointer_cast<QuantLib::OptionletStripper> (*_ppOptionletStripper2));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper2::COptionletStripper2 (QuantLib::OptionletStripper2 childNative)  : COptionletStripper(COptionletStripper2::typeid)
{
#ifdef HANDLE
	_phOptionletStripper2 = NULL;
#endif
	_ppOptionletStripper2 = new boost::shared_ptr<QuantLib::OptionletStripper2> (new QuantLib::OptionletStripper2 (childNative));
    _ppOptionletStripper = new boost::shared_ptr<QuantLib::OptionletStripper> (boost::dynamic_pointer_cast<QuantLib::OptionletStripper> (*_ppOptionletStripper2));
}
#endif

Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper2::~COptionletStripper2 ()
{
    if (_ppOptionletStripper2 != NULL)
    {
	    delete _ppOptionletStripper2;
        _ppOptionletStripper2 = NULL;
    }
}
Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper2::!COptionletStripper2 ()
{
    if (_ppOptionletStripper2 != NULL)
    {
	    delete _ppOptionletStripper2;
    }
}
QuantLib::OptionletStripper2& Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper2::GetReference ()
{
    if (_ppOptionletStripper2 == NULL) throw REFNEW NativeNullException ();
	return **_ppOptionletStripper2;
}
boost::shared_ptr<QuantLib::OptionletStripper2>& Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper2::GetShared ()
{
    if (_ppOptionletStripper2 == NULL) throw REFNEW NativeNullException ();
	return *_ppOptionletStripper2;
}
QuantLib::OptionletStripper2* Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper2::GetPointer ()
{
    if (_ppOptionletStripper2 == NULL) throw REFNEW NativeNullException ();
	return &**_ppOptionletStripper2;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::OptionletStripper2>& Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper2::GetHandle ()
{
	if (_phOptionletStripper2 == NULL)
	{
		_phOptionletStripper2 = new Handle<QuantLib::OptionletStripper2> (*_ppOptionletStripper2);
	}
	return *_phOptionletStripper2;
}
#endif
bool Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper2::HasNative () 
{
	return (_ppOptionletStripper2 != NULL);
}

Cephei::Core::IVector<Double>^ Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper2::AtmCapFloorPrices::get ()
{
    try
    {
    	std::vector<QuantLib::Real> _rv = (std::vector<QuantLib::Real>)(*_ppOptionletStripper2)->atmCapFloorPrices ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper2::AtmCapFloorStrikes::get ()
{
    try
    {
    	std::vector<QuantLib::Rate> _rv = (std::vector<QuantLib::Rate>)(*_ppOptionletStripper2)->atmCapFloorStrikes ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::Volatility::Optionlet::IOptionletStripper2^ Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper2::PerformCalculations::get ()
{
    try
    {
    	(*_ppOptionletStripper2)->performCalculations ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper2::SpreadsVol::get ()
{
    try
    {
    	std::vector<QuantLib::Volatility> _rv = (std::vector<QuantLib::Volatility>)(*_ppOptionletStripper2)->spreadsVol ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Termstructures::Volatility::Optionlet::IOptionletStripper2^ Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper2_Factory::Create (Cephei::QL::Termstructures::Volatility::Optionlet::IOptionletStripper1^ optionletStripper1, Cephei::QL::Termstructures::Volatility::Capfloor::ICapFloorTermVolCurve^ atmCapFloorTermVolCurve)
{
    return REFNEW COptionletStripper2 ( optionletStripper1,  atmCapFloorTermVolCurve);
}
